package org.example.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Result;
import org.example.utils.AliyunOSSOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    /*
    * 本地磁盘存储
    * */
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile file) throws IOException {
//        log.info("上传文件：username = {}, age = {}, file = {}", username, age, file);
//        //获取原始文件名
//        String originalFilename = file.getOriginalFilename();
//        //获取文件后缀名
//        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String newFilename = UUID.randomUUID().toString() + extension;
//
//        file.transferTo(new File("upload/" + newFilename));
//
//        return Result.success();
//    }

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("上传文件：file = {}", file.getOriginalFilename());
        //将文件交给oss存储管理
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传成功，url = {}", url);
        return Result.success(url);
    }


}
